package com.kara.webapp.pages.admin.clazz;

import java.util.List;

import org.apache.tapestry5.annotations.ActivationRequestParameter;
import org.apache.tapestry5.annotations.Cached;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;

import com.kara.entity.Clazz;
import com.kara.entity.Trinn;
import com.kara.service.ClazzManager;
import com.kara.webapp.components.clazz.ClazzForm;

public class ClazzEdit {

	@ActivationRequestParameter
	private long schoolYearId;
	
	private long id;
	
	@Property @Persist
	private Clazz clazz;
	
	@Inject
	private ClazzManager clazzManager;
	
	@InjectPage 
	private ClazzAdmin clazzAdmin;
	
	@Component(id="clazzForm")
	private ClazzForm clazzForm;
	
	public void setId(long id) {
		this.id = id;
	}
	void beginRender(){
		if (id != 0){
			clazz = clazzManager.getClazz(id);
		}else {
			clazz = new Clazz();
			clazz.setSchoolYear(clazzManager.getCurrentSchoolYear());
		}
	}
	
	@Cached
	public List<String> getTrinn() {
		List<String>list = CollectionFactory.newList();
		for (Trinn t : Trinn.values()) {
			list.add(t.name());
		}
		return list;
	}
	
	Object onSuccess(){
		return clazzAdmin;
	}
	
	Object onPassivate(){
		return id;
	}
	
	void onActivate(long id){
		this.id = id;
	}
}
